/**
 * 
 */
package com.mh.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mh.domain.AppRole;
import com.mh.domain.Customer;
import com.mh.service.AppRoleService;
import com.mh.web.utils.CustomGenericResponse;
import com.mh.web.utils.Page;

/**
 * @author VIT DEMO
 *
 */

@Controller
@RequestMapping(value="/approle" )
public class AppRoleController {

	@Autowired
	private AppRoleService appRoleService;
	
	private Validator validator;
	
	@Autowired
	public AppRoleController(Validator validator) {
		this.validator = validator;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(){
		return "role";
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public AppRole read(@PathVariable("id") Integer id){
		return appRoleService.findAppRoleByPrimaryKey(id);		
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody Page<AppRole> listRoles(AppRole approle){
		List<AppRole> list = new ArrayList<AppRole>(appRoleService.findAppRoles(approle));
		Page<AppRole> pg = new Page<AppRole>();
		
		pg.setRows(list);


		return pg;
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.PUT)
	public @ResponseBody CustomGenericResponse delete(@PathVariable("id") int id){
		CustomGenericResponse resp = new CustomGenericResponse();
		try {
			AppRole approle = appRoleService.findAppRoleByPrimaryKey(id);
			appRoleService.deleteAppRole(approle);
			resp.setSuccess(true);
			resp.setMessage("Success");
		} catch (Exception e) {
			resp.setSuccess(true);
			resp.setMessage("Error");
		}		
		return resp;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
	public @ResponseBody CustomGenericResponse  update(
			@RequestBody AppRole approle, @PathVariable("id") int id){
		CustomGenericResponse resp = new CustomGenericResponse();
		List<String> errors = validate(approle);
		if(!errors.isEmpty()){
			resp.setSuccess(false);			
			resp.setMessages(errors);
			return resp;
		}		
		appRoleService.saveAppRole(approle);
		return resp;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.PUT)	
	public @ResponseBody CustomGenericResponse  save(
			@RequestBody AppRole approle){
		CustomGenericResponse resp = new CustomGenericResponse();
		List<String> errors = validate(approle);
		
		if(!errors.isEmpty()){
			resp.setSuccess(false);			
			resp.setMessages(errors);
			return resp;
		}		
		appRoleService.saveAppRole(approle);
		return resp;
	}
	
	private List<String> validate(AppRole approle){
		List<String> errors = new ArrayList<String>();
		Set<ConstraintViolation<AppRole>> violations = validator.validate(approle);
		for (ConstraintViolation<AppRole> violation : violations) 
			errors.add(violation.getMessage());			   
		return errors;		
	}

	@SuppressWarnings("unused")
	private List<String> getMessages(BindingResult result){
		List<String> errors = new ArrayList<String>();
			for(ObjectError error: result.getAllErrors()){
				errors.add(error.getCode() + " - "+  error.getDefaultMessage());
			}
		return errors;
	}	

	public AppRoleService getAppRoleService() {
		return appRoleService;
	}

	public void setAppRoleService(AppRoleService appRoleService) {
		this.appRoleService = appRoleService;
	}
	

}
